package com.gree.scmservice.dao;

import com.gree.common.entity.SysOperLog;
import com.gree.common.entity.TablePageData;
import com.gree.scmservice.entity.saas.SaasSysAddon;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by 180686 on 2021/10/30 11:38
 */
@Repository
public class OperLogDao {

    @Autowired
    private Dao dao;

    public TablePageData<SysOperLog> displayOperLog(SysOperLog sysOperLog){
        Pager pager = dao.createPager(sysOperLog.getPageNum(), sysOperLog.getPageSize());
        sysOperLog.setBaseEntityNull();
        Cnd cnd = Cnd.from(dao, sysOperLog);
        List query = dao.query(SaasSysAddon.class, cnd, pager);
        pager.setRecordCount(dao.count(SaasSysAddon.class, cnd));
        TablePageData tablePageData = new TablePageData(query, pager);
        return tablePageData;
    }


    public int getOperLogCount(String comp, String filter){
        Sql sql = Sqls.create("select count(*) as nums from sys_oper_log oper,$smbse01 sb1,$smcom09 sc09" +
                " where oper.oper_name = sb1.usid and oper.comp = sc09.comp " + filter);
        sql.setVar("smbse01","smbse01"+comp).setVar("smcom09","smcom09");
        sql.setCallback(Sqls.callback.entity());
        sql.setEntity(dao.getEntity(Record.class));
        dao.execute(sql);
        Record countRecord=(Record) sql.getResult();
        int count= countRecord.getInt("nums");
        return count;
    }

    public TablePageData<Record> displayOperLog(String comp, String filter, int count, int page, int pageSize){
        Sql sql = Sqls.create("select sb1.nama,oper.*,sc09.dsca from sys_oper_log oper,$smbse01 sb1,$smcom09 sc09" +
                " where oper.oper_name = sb1.usid and oper.comp = sc09.comp " + filter);
        Pager pager = dao.createPager(page,pageSize);
        pager.setRecordCount(count);
        sql.setVar("smbse01","smbse01"+comp).setVar("smcom09","smcom09");
        sql.setPager(pager);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(Record.class));
        List<Record> dataList=dao.execute(sql).getList(Record.class);
        return new TablePageData(dataList, pager);
    }

    public int deleteOperinforByIds(Long[] infoIds){
        int result = dao.clear("sys_oper_log", Cnd.where("id","in",infoIds));
        return result;
    }

    public int cleanOperinfor(){
        int result = dao.clear("sys_oper_log");
        return result;
    }

}
